package org.stvd.repository.admin.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.stvd.core.dto.QueryResult;
import org.stvd.entities.admin.Resources;
import org.stvd.repository.admin.ResourcesDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

@Repository("ResourcesDao")
public class ResourcesDaoImpl extends BaseDaoImpl<Resources> implements ResourcesDao {

    @Override
    public List<Resources> findResourceAll(String guid) {
        return findByHQL("from Resources T where T.guid = ?0 order by T.type desc,T.priority asc", guid);
    }
    
    @Override
    public List<Resources> findResourcesByUid(String userId, String guid){
        String hql = "SELECT DISTINCT T1 FROM Resources T1, UserRole T2, RoleRes T3"
            + " WHERE T1.resId = T3.resId AND T2.roleId = T3.roleId "
            + " AND T2.userId = ?0 and T2.guid = ?1"
            + " ORDER BY T1.priority ";
        return findByHQL(hql, userId, guid); 
    }

    @Override
    public List<Resources> findResourcesByRoleId(String roleId, String guid) {
        return findByHQL("select T1 from Resources T1, RoleRes T2"
            + " WHERE T1.resId = T2.resId AND T2.roleId = ?0 and T2.guid = ?1", roleId, guid);
    }

    @Override
    public List<Resources> findResourcesByType(String type, String guid) {
        return findByHQL("from Resources where type = ?0 and guid = ?1 ", type, guid);
    }
    
    @Override
    public QueryResult<Resources> getResourceQueryResult(int start, int limit, String type, String guid) {
        return getQueryResultByHQL(start, limit, "from Resources where type = ?0 and guid = ?1 order by priority desc", type, guid);
    }
}
